
#include "queue.h"

linkqueue * init_queue()
{
    linkqueue *q = calloc(1, sizeof(linkqueue));
    if(q != NULL)
    {
        q->head = NULL;
        q->tail = NULL;
        q->number = 0;
    }
    return q;
}

node *new_queue_node(int data)
{
    node *new = calloc(1, sizeof(node));
    if(new != NULL)
    {
        new->data = data;
        new->next = NULL;
    }
    return new;
}

bool queue_is_empty(linkqueue *q)
{
    return q->number == 0;
}

void en_quene(linkqueue *q, int data)
{
    node *new = new_queue_node(data);
    if(new == NULL)
        return;

    if(queue_is_empty(q))
        q->head = new;
    else
        q->tail->next = new;

    q->tail = new;
    q->number++;
}

node * out_queue(linkqueue *q)
{
    if(queue_is_empty(q))
        return NULL;

    node *tmp = q->head;

    q->head = q->head->next;
    if(q->head == NULL)
        q->tail = NULL;

    tmp->next = NULL;
    return tmp;
}

node *queue_front(linkqueue *q)
{
    return queue_is_empty(q) ? NULL : q->head;
}